package com.ly.hotel.view.fragments.music;

import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.MusicAlbumBean;
import com.ly.hotel.model.MusicBean;
import com.ly.hotel.util.ImageUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by SHUBEN on 2018/1/26.
 */

public class MusicListDialog extends Dialog {
    private MyApplication app;
    private MusicActivity mcontext;
    private MusicAlbumBean mAlbumbean;
    private ImageView albumIco;
    private TextView albumName;
    private TextView albumMusicNum;// 例：全部歌曲(70)首
    private Button albumPlayAll;// 整张专辑加入列表
    private ListView musicAlbumMusicListView;
    private ImageView mdownMore;
    private List<MusicBean> musicAlbumMusicList = new ArrayList<MusicBean>();
    private MusicAlbumMusicListAdapter mAlbumMusicListAdapter;


    public MusicListDialog(MusicActivity context) {
        super(context);
        this.mcontext = context;
    }

    public MusicListDialog(MusicActivity context, int theme,MusicAlbumBean b) {
        super(context, theme);
        this.mcontext = context;
        this.mAlbumbean = b;
        app = (MyApplication) mcontext.getApplicationContext();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_list);
        albumIco = (ImageView) findViewById(R.id.m_album_detial_img);
        albumName = (TextView) findViewById(R.id.m_album_detial_name);
        albumMusicNum = (TextView) findViewById(R.id.m_album_detial_musicNum);
        albumPlayAll = (Button) findViewById(R.id.m_album_detial_playAll_btm);
        musicAlbumMusicListView = (ListView) findViewById(R.id.musicAlbumMusicListView);
        mdownMore = (ImageView) findViewById(R.id.music_list_more);
        Button backBtn = (Button) findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
            }
        });

        musicAlbumMusicListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                // TODO Auto-generated method stub
            }

            /* (non-Javadoc)
             * @see android.widget.AbsListView.OnScrollListener#onScroll(android.widget.AbsListView, int, int, int)
             */
            @Override
            public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                if ((arg1 == 0 && arg2 == arg3) || (arg1 > 0 && arg1+arg2 == arg3)) {
                    mdownMore.setVisibility(View.INVISIBLE);
                }  else {
                    mdownMore.setVisibility(View.VISIBLE);
                }
            }
        });

        albumPlayAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mcontext.addMusicToPlayList(app.getMusics(mAlbumbean.getAlbumId()),-1);
            }
        });

        albumName.setText(mAlbumbean.getAlbumName());
        albumMusicNum.setText(mcontext.getString(R.string.music_album_count) + "(" + mAlbumbean.getMusicPages().get(0).getTotalRecord() + ")");
        albumPlayAll.setText(mcontext.getString(R.string.music_play_all));
        ImageUtil.displayImage_Round(mcontext, MyApplication.getHostResUrl() + mAlbumbean.getImg(),
                albumIco,mcontext.getResources().getDimensionPixelSize(R.dimen.px15));
        setMusicToAlbumMusicList(app.getMusics(mAlbumbean.getAlbumId()));
    }

    private void setMusicToAlbumMusicList(List<MusicBean> mb) {

        musicAlbumMusicList = new ArrayList<MusicBean>();
        musicAlbumMusicList.addAll(mb);
        mAlbumMusicListAdapter = new MusicAlbumMusicListAdapter(mcontext, musicAlbumMusicList);

        // 使用自定义的Adapter
        musicAlbumMusicListView.setItemsCanFocus(true);
        musicAlbumMusicListView.setAdapter(mAlbumMusicListAdapter);

        //请求焦点
        musicAlbumMusicListView.post(new Runnable(){
            public void run(){
                int count = musicAlbumMusicListView.getChildCount();
                if (count != 0){
                    musicAlbumMusicListView.requestFocus();
                    musicAlbumMusicListView.requestFocusFromTouch();
                    musicAlbumMusicListView.setSelection(0);
                }
            }

        });
    }

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event) {
        return mcontext.onKeyDown(keyCode,event);
    }
}